package xyz.riceball.beans;

/**
 * <p>
 * bean定义有了就需要实现bean定义接口BeanDefinition
 * 实现一个通用的bean定义GenericBeanDefinition,用来承载数据
 * </p>
 *
 * @author xiaovcloud
 * @since 2021/6/29 19:40
 */
public class GenericBeanDefinition implements BeanDefinition {


    /**
     * bean的名称
     */
    private Class<?> beanClass;

    /**
     * 工厂bean名
     */
    private String factoryBeanName;

    /**
     * 工厂方法名
     */
    private String factoryMethodName;

    /**
     * 初始化方法
     */
    private String initMethodName;

    /**
     * 销毁方法
     */
    private String destroyMethodName;

    @Override
    public Class<?> getBeanClass() {
        return this.beanClass;
    }

    @Override
    public String getFactoryBeanName() {
        return this.factoryBeanName;
    }

    @Override
    public String getFactoryMethodName() {
        return this.factoryMethodName;
    }

    @Override
    public String getInitMethodName() {
        return this.initMethodName;
    }

    @Override
    public String getDestroyMethodName() {
        return this.destroyMethodName;
    }

    /**
     * 设置bean的class
     *
     * @param beanClass bean类型
     */
    public void setBeanClass(Class<?> beanClass) {
        this.beanClass = beanClass;
    }

    /**
     * 设置工厂bean名
     *
     * @param factoryBeanName bean工厂名
     */
    public void setFactoryBeanName(String factoryBeanName) {
        this.factoryBeanName = factoryBeanName;
    }

    /**
     * 设置工厂方法名
     *
     * @param factoryMethodName 方法名
     */
    public void setFactoryMethodName(String factoryMethodName) {
        this.factoryMethodName = factoryMethodName;
    }

    /**
     * 设置初始化方法
     *
     * @param initMethodName 方法名
     */
    public void setInitMethodName(String initMethodName) {
        this.initMethodName = initMethodName;
    }

    /**
     * 设置销毁方法
     *
     * @param destroyMethodName 方法名
     */
    public void setDestroyMethodName(String destroyMethodName) {
        this.destroyMethodName = destroyMethodName;
    }

    @Override
    public String toString() {
        return "GenericBeanDefinition [beanClass=" + beanClass + ", factoryBeanName="
                + factoryBeanName + ", factoryMethodName=" + factoryMethodName + ", initMethodName=" + initMethodName
                + ", destroyMethodName=" + destroyMethodName + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((beanClass == null) ? 0 : beanClass.hashCode());
        result = prime * result + ((destroyMethodName == null) ? 0 : destroyMethodName.hashCode());
        result = prime * result + ((factoryBeanName == null) ? 0 : factoryBeanName.hashCode());
        result = prime * result + ((factoryMethodName == null) ? 0 : factoryMethodName.hashCode());
        result = prime * result + ((initMethodName == null) ? 0 : initMethodName.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        GenericBeanDefinition other = (GenericBeanDefinition) obj;
        if (beanClass == null) {
            if (other.beanClass != null) {
                return false;
            }
        } else if (!beanClass.equals(other.beanClass)) {
            return false;
        }
        if (destroyMethodName == null) {
            if (other.destroyMethodName != null) {
                return false;
            }
        } else if (!destroyMethodName.equals(other.destroyMethodName)) {
            return false;
        }
        if (factoryBeanName == null) {
            if (other.factoryBeanName != null) {
                return false;
            }
        } else if (!factoryBeanName.equals(other.factoryBeanName)) {
            return false;
        }
        if (factoryMethodName == null) {
            if (other.factoryMethodName != null) {
                return false;
            }
        } else if (!factoryMethodName.equals(other.factoryMethodName)) {
            return false;
        }
        if (initMethodName == null) {
            return other.initMethodName == null;
        } else {
            return initMethodName.equals(other.initMethodName);
        }
    }

}
